package h0910;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 行星碰撞
 * @date 2024/9/10 23:43
 */
public class PlanetCollide {
    public static void main(String[] args) {
        int[] nums = {5, 10, -5};
        int[] ints = asteroidCollision(nums);
        System.out.println(Arrays.toString(ints));
    }

    public static int[] asteroidCollision(int[] asteroids) {
        Deque<Integer> stack = new LinkedList<>();

        for (int i = 0; i < asteroids.length; i++) {
            boolean had=false;
            while (!stack.isEmpty() && asteroids[i] < 0 && stack.peek() > 0) {
                int a = asteroids[i] + stack.peek();
                if (a < 0) {
                    stack.pop();
                } else if (a == 0) {
                    stack.pop();
                    had=true;
                    break;
                } else {
                    had=true;
                    break;
                }
            }
            if (!had){
                stack.push(asteroids[i]);
            }
        }
        int[] res = new int[stack.size()];
        for (int i = res.length - 1; i >= 0; i--) {
            res[i] = stack.pop();
        }
        return res;
    }
}
